## Pseudo-code for building the diff-in-diff dataset
import json
import random

random.seed('09-21-2016')

execfile("./Dropbox/Coalitions/loadCommitteeData.py")
execfile("./Dropbox/Coalitions/cueingFunctions.py")
execfile("./Dropbox/Coalitions/getMatchData.py")

# Get cosponsorship data
cosponsorship = {}
with open("./Dropbox/Coalitions/cosponsorship.json", 'r') as dataf:
    cosponsorship = json.load(dataf)

congress_range = xrange(96,114)
#getMatchData(cosponsorship, congress_range)

file_stem = '1616split'
# Hard-coded for convenience
execfile("./Dropbox/Coalitions/getMatchSDs.py")
set_globals(16, 16, sdX)
sample = sorted(sample, key = lambda obs : obs[2])
    
last_iter_comm = ''
last_iter_cong = ''
agreement_data = {}
match_data = {}
givers_in_sample = 0

print(len([x for x in sample if x[3] >= 96 and x[3] <= 113]))

# Collect matched diff-in-diff data
random.seed('09-21-2016')
with open('./Dropbox/Coalitions/cueing_data' + file_stem + '.csv', 'w') as outf:
    outf.write('agree1,agree2,n1,n2,treat,cs,congress,copartisans,leg_party,sgcs,ogcs,sfcs,ofcs,chamber,committee,legA,legB,matched\n')

    for unit in sample:
        leg = unit[0]
        chamber = unit[1]
        new_committee = unit[2]
        congress = unit[3]
        
        if congress < 96 or congress > 113 or \
        leg not in all_legislators[chamber][congress-1] or \
        leg not in all_legislators[chamber][congress+1]:
            continue
        
        # Get voting data
        if new_committee != last_iter_comm:
            del agreement_data
            with open("./Dropbox/Coalitions/agreement/agree-" + new_committee +".json", 'r') as agf:
                agreement_data = json.load(agf)

        # Get the matched data        
        if congress != last_iter_cong:
            del match_data
            with open("./Dropbox/Coalitions/match/agmatch" + '-' + str(congress) + ".json", 'r') as dataf:
                match_data = json.load(dataf)
                    
        last_iter_comm = new_committee
        last_iter_cong = congress

        constructDiDData(leg, chamber, new_committee, congress, \
        agreement_data, match_data, cosponsorship, outf)

random.seed('09-21-2016')
# Collect parallel_trends data
with open('./Dropbox/Coalitions/pt_cueing_data' + file_stem + '.csv', 'w') as outf:
    outf.write('agree1,agree2,n1,n2,treat,cs,congress,copartisans,leg_party,sgcs,ogcs,sfcs,ofcs,chamber,committee,legA,legB,matched\n')

    for unit in sample:
        leg = unit[0]
        chamber = unit[1]
        new_committee = unit[2]
        congress = unit[3] - 2
        
        if congress < 96 or congress > 113 or \
        leg not in all_legislators[chamber][congress-1] or \
        leg not in all_legislators[chamber][congress+1]:
            continue
        
        # Get voting data
        if new_committee != last_iter_comm:
            del agreement_data
            with open("./Dropbox/Coalitions/agreement/agree-" + new_committee +".json", 'r') as agf:
                agreement_data = json.load(agf)

        # Get the matched data        
        if congress != last_iter_cong:
            del match_data
            with open("./Dropbox/Coalitions/match/agmatch" + '-' + str(congress) + ".json", 'r') as dataf:
                match_data = json.load(dataf)
                    
        last_iter_comm = new_committee
        last_iter_cong = congress

        constructDiDData(leg, chamber, new_committee, congress, \
        agreement_data, match_data, cosponsorship, outf)

random.seed('09-21-2016')
# Collect the exit sample
with open('./Dropbox/Coalitions/exit_cueing_data' + file_stem + '.csv', 'w') as outf:
    outf.write('agree1,agree2,n1,n2,treat,cs,congress,copartisans,leg_party,sgcs,ogcs,sfcs,ofcs,chamber,committee,legA,legB,matched\n')
    for unit in exit_sample:
        leg = unit[0]
        chamber = unit[1]
        new_committee = unit[2]
        congress = unit[3]
        
        if congress < 96 or congress > 113 or \
        leg not in all_legislators[chamber][congress-1] or \
        leg not in all_legislators[chamber][congress+1]:
            continue
        
        # Get voting data
        if new_committee != last_iter_comm:
            del agreement_data
            with open("./Dropbox/Coalitions/agreement/agree-" + new_committee +".json", 'r') as agf:
                agreement_data = json.load(agf)

        # Get the matched data        
        if congress != last_iter_cong:
            del match_data
            with open("./Dropbox/Coalitions/match/agmatch" + '-' + str(congress) + ".json", 'r') as dataf:
                match_data = json.load(dataf)
                    
        last_iter_comm = new_committee
        last_iter_cong = congress

        constructDiDData(leg, chamber, new_committee, congress, \
        agreement_data, match_data, cosponsorship, outf)